#include "dialog.h"
#include "ui_dialog.h"
#include <QDebug>
#include <QDate>
#include <QTime>
#include "mainmodedecrypt.h"


Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{

    ui->setupUi(this);
    setWindowIcon(QIcon(":/logo.png"));
    setWindowTitle("");
    QRegExp rx("^[0-9]{1,12}$");//这个10就是最大长度
    QValidator *validator = new QRegExpValidator(rx,0);
    ui->lineEdit_Serial->setValidator(validator);
    QRegExp rx1("^[0-9]{1,10}$");//这个10就是最大长度
    QValidator *validator1 = new QRegExpValidator(rx1,0);
    ui->lineEdit_Date->setValidator(validator1);
    //ui->lineEdit_Builder->setEnabled(false);

    msgBox.setWindowTitle(QString::fromLocal8Bit("提示"));
    msgBox.setIcon(QMessageBox::Information);
    msgBox.addButton(QString::fromLocal8Bit("确定"),QMessageBox::YesRole);

    //模块名称添加
    //ui->comboBox_modeName->addItem(QString("主块"));

    m_pCale = new QCalendarWidget();
    m_pCale->setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);

    connect(m_pCale,SIGNAL(selectionChanged()),this,SLOT(onSelected()));
}

Dialog::~Dialog()
{
    delete ui;
}
bool Dialog::checkDate(QString text2)
{
    if(text2 =="20121212")
    {
        msgBox.setText(QString::fromLocal8Bit("确定要永久激活吗？"));
        msgBox.exec();
        return true;
    }
    int year = text2.left(4).toInt();
    int month = text2.mid(4,2).toInt();
    int day = text2.mid(6,2).toInt();
    //int hour = text2.right(2).toInt();

    QDate currentDate = QDate::currentDate();
    //QTime currentTime = QTime::currentTime();
    int currentyear = currentDate.year();
    int currentmonth = currentDate.month();
    int currentday = currentDate.day();
    //int currenthour = currentTime.hour();

    QDate date1(year,month,day);
    QDate date2(currentyear,currentmonth,currentday);
    if(date1 <= date2)
    {
        //if(hour <= currenthour)
       // {
            msgBox.setText(QString::fromLocal8Bit("激活时间应大于当前时间！"));
            msgBox.exec();
            return false;
       // }
    }

    return true;
}



void Dialog::on_BuilderBtn_clicked()
{
    int countS = ui->lineEdit_Serial->text().count();
    int countD = ui->lineEdit_Date->text().count();
    if(countS != 12 )
    {
        QMessageBox::information(this,"提示","序列号长度错误",QMessageBox::Yes);
        return;
    }
    if( countD != 8)
    {
        QMessageBox::information(this,"提示","日期格式错误",QMessageBox::Yes);
        return;
    }
    QString lockDate = ui->lineEdit_Date->text();
    if(!checkDate(lockDate))
    {
        QMessageBox::information(this,"提示","日期格式错误",QMessageBox::Yes);
        return;
    }
    QString controllerSerialId = ui->lineEdit_Serial->text();
    //根据机器id和锁机日期生成注册码
    QString registerNumber = objModeHandle.objMainModeEncrypt.encodeRegisterNumber(controllerSerialId,lockDate);
    ui->lineEdit_Builder->setText(registerNumber);
    //
    MainModeDecrypt objMainModeDecrypt;
    QString strNumOut,strdataOut;
    objMainModeDecrypt.decryptLicense(registerNumber,strNumOut,strdataOut);//根据注册码提取序列号和锁机日期
    qDebug()<<"strNumOut"<<strNumOut<<" strdataOut"<<strdataOut;
    QMessageBox::information(this,"提示","成功",QMessageBox::Yes);

}



void Dialog::on_calendarBtn_clicked()
{
    m_pCale->show();

}

void Dialog::onSelected()
{
    int year = m_pCale->selectedDate().year();
    int month  = m_pCale->selectedDate().month();
    int day = m_pCale->selectedDate().day();

    QString y = QString::number(year);
    QString m = QString::number(month);
    QString d = QString::number(day);
    if(m.count() == 1 )
    {
        m = "0"+m;
    }
    if(d.count() == 1)
    {
        d = "0"+d;
    }
    ui->lineEdit_Date->setText(y+m+d);
}

void Dialog::on_comboBox_modeName_currentIndexChanged(int index)
{
    if(index >= ModeHandle::E_MODETYPE_MAIN && index < ModeHandle::E_MODETYPE_END)
    {
        objModeHandle.emModeType = static_cast<ModeHandle::E_ModeType>(index);
    }
    else
    {
        QMessageBox::about(NULL, QString::fromLocal8Bit("提示"),QString::fromLocal8Bit("模块类型未定义！"));
    }
}
